package com.gb28181.commons;

public class PtzCmdHelper {
   /**
    * leftRight  镜头左移右移 0:停止 1:左移 2:右移
    * upDown     镜头上移下移 0:停止 1:上移 2:下移
    * inOut      镜头放大缩小 0:停止 1:缩小 2:放大
    * moveSpeed  镜头移动速度 默认 0XFF (0-255)
    * zoomSpeed  镜头缩放速度 默认 0X1 (0-255)
    */
    public static String create(int leftRight, int upDown, int inOut, int moveSpeed, int zoomSpeed) {
            int cmdCode = 0;
            if (leftRight == 2) {
                cmdCode|=0x01;      // 右移
            } else if(leftRight == 1) {
                cmdCode|=0x02;  // 左移
            }
            if (upDown == 2) {
                cmdCode|=0x04;         // 下移
            } else if(upDown == 1) {
                cmdCode|=0x08;     // 上移
            }
            if (inOut == 2) {
                cmdCode |= 0x10;        // 放大
            } else if(inOut == 1) {
                cmdCode |= 0x20;    // 缩小
            }
            StringBuilder builder = new StringBuilder("A50F01");
            String strTmp;
            strTmp = String.format("%02X", cmdCode);
            builder.append(strTmp, 0, 2);
            strTmp = String.format("%02X", moveSpeed);
            builder.append(strTmp, 0, 2);
            builder.append(strTmp, 0, 2);
            strTmp = String.format("%X", zoomSpeed);
            builder.append(strTmp, 0, 1).append("0");
            //计算校验码
            int checkCode = (0XA5 + 0X0F + 0X01 + cmdCode + moveSpeed + moveSpeed + (zoomSpeed << 4 & 0XF0)) % 0X100;
            strTmp = String.format("%02X", checkCode);
            builder.append(strTmp, 0, 2);
            return builder.toString();
    }
}
